package com.tangbuzhi.strategy;

/**
 * 策略模式案例
 */
public class StrategyCase {
    public static void main(String[] args) {
        //原始方案
        System.out.println(Calculator.calculate(7, 5, Calculator.ADD));
        System.out.println(Calculator.calculate(7, 5, Calculator.SUBTRACT));
        System.out.println(Calculator.calculate(7, 5, Calculator.MULTIPLY));
        System.out.println(Calculator.calculate(7, 5, Calculator.DIVIDE));

        System.out.println("============================");
        //策略模式修改方案
        StrategyCalculator strategyCalculator = new StrategyCalculator();
        System.out.println(strategyCalculator.setStrategy(new Add()).calculate(7, 5));
        System.out.println(strategyCalculator.setStrategy(new Subtract()).calculate(7, 5));
        System.out.println(strategyCalculator.setStrategy(new Multiply()).calculate(7, 5));
        System.out.println(strategyCalculator.setStrategy(new Divide()).calculate(7, 5));
        System.out.println(strategyCalculator.setStrategy(new Mod()).calculate(7, 5));
    }

    public static class Calculator {
        private static final String ADD = "+";
        private static final String SUBTRACT = "-";
        private static final String MULTIPLY = "*";
        private static final String DIVIDE = "/";

        public static float calculate(float a, float b, String type) {
            switch (type) {
                case ADD:
                    return a + b;
                case SUBTRACT:
                    return a - b;
                case MULTIPLY:
                    return a * b;
                case DIVIDE:
                    return a / b;
                default:
                    return 0;
            }
        }
    }

    public interface CalculateMethod {
        float calculate(float a, float b);
    }

    public static class Add implements CalculateMethod {
        @Override
        public float calculate(float a, float b) {
            return a + b;
        }
    }

    public static class Subtract implements CalculateMethod {
        @Override
        public float calculate(float a, float b) {
            return a - b;
        }
    }

    public static class Multiply implements CalculateMethod {
        @Override
        public float calculate(float a, float b) {
            return a * b;
        }
    }

    public static class Divide implements CalculateMethod {
        @Override
        public float calculate(float a, float b) {
            return a / b;
        }
    }

    public static class StrategyCalculator {
        private CalculateMethod method;

        public StrategyCalculator setStrategy(CalculateMethod method) {
            this.method = method;
            return this;
        }

        public float calculate(float a, float b) {
            return method.calculate(a, b);
        }
    }

    public static class Mod implements CalculateMethod {
        @Override
        public float calculate(float a, float b) {
            return a % b;
        }
    }
}
